//
//  XMRegistOneViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/22.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMRegistTwoViewController.h"
#import <BRPickerView/BRPickerView.h>
#import "XMLoginPrivacyTipView.h"
#import "XMRegistThreeViewController.h"
#import "XMLoginRecordView.h"
#import "XMHeightChooseView.h"


@interface XMRegistTwoViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgV;
@property (weak, nonatomic) IBOutlet UILabel *xueliLa;
@property (weak, nonatomic) IBOutlet UILabel *zhiyeLa;
@property (weak, nonatomic) IBOutlet UILabel *shengaoLa;
@property (weak, nonatomic) IBOutlet UITextField *signField;
@property (weak, nonatomic) IBOutlet UIView *voiceBgV;
@property (weak, nonatomic) IBOutlet UILabel *luzhiLa;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@property (nonatomic, strong) NSMutableArray *xueliArr;
@property (nonatomic, strong) NSMutableArray *zhiyeArr;
@property (nonatomic, strong) XMLoginPrivacyTipView *tipView;
@property (nonatomic, strong) XMHeightChooseView *heightChooseV;
@property (nonatomic, copy) NSString *voicePath;
@property (nonatomic, assign) NSInteger voiceLength;
@end

@implementation XMRegistTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navView.backgroundColor = kHEXA(@"ffffff", 0);
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    [self.navRightBtn setTitle:@"2/3" forState:UIControlStateNormal];
    [self.navRightBtn setTitleColor:kAppMainColor forState:UIControlStateNormal];
    
    kCornerRadius(_bgV, 10, 0, UIColor.clearColor);
    kCornerRadius(_voiceBgV, 10, 0, UIColor.clearColor);
    kCornerRadius(_nextBtn, 21, 0, UIColor.clearColor);
    
    self.signField.delegate = self;
    
    XMWeakSelf(self);
    [self.tipView tipViewLeftBlock:^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self showRecordView];
        }];
        
    } rightBlock:^{
        [GKCover hideCover];
    }];
    
    self.heightChooseV.cancelBlock = ^{
        [GKCover hideCover];
    };
    
    self.heightChooseV.sureBlock = ^(NSString * _Nonnull hStr, NSString * _Nonnull wStr) {
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            self.shengaoLa.text = kStringFormat(@"%@-%@", hStr, wStr);
        }];
    };
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

- (IBAction)nextAct:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self.zhiyeLa.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showHUDMsg:@"请选择职业!" toView:nil];
        return;
    }
    if ([self.xueliLa.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showHUDMsg:@"请选择学历!" toView:nil];
        return;
    }
    NSString *shengao = @"";
    NSString *tizhong = @"";
    if ([self.shengaoLa.text containsString:@"-"]) {
        NSString *str = self.shengaoLa.text;
        str = [str stringByReplacingOccurrencesOfString:@"cm" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"kg" withString:@""];
        NSArray *arr = [str componentsSeparatedByString:@"-"];
        shengao = [arr firstObject];
        tizhong = [arr lastObject];
    }
    
    XMRegistThreeViewController *vc = [[XMRegistThreeViewController alloc] initWithNibName:kStringFromClass(XMRegistThreeViewController) bundle:nil];
    
    vc.gender = self.gender;
    vc.nickname = self.nickname;
    vc.age = self.age;
    vc.city = self.city;
    vc.invite_code = self.invite_code;
    
    vc.recordPath = self.voicePath;
    vc.recordTime = self.voiceLength;
    vc.zhiye = self.zhiyeLa.text;
    vc.xueli = self.xueliLa.text;
    vc.shengao = shengao;
    vc.tizhong = tizhong;
    vc.sign = self.signField.text;
    vc.content = self.content;
    vc.person_id = self.person_id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)shengaoAct:(UIButton *)sender {
    [self.view endEditing:YES];
    [self showHeightChooseV];
}

- (IBAction)xueliAct:(UIButton *)sender {
    [self.view endEditing:YES];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc] init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = @"选择学历";
    stringPickerView.dataSourceArr = [self xueliArr];
    if (self.xueliLa.text.length > 0) {
        stringPickerView.selectIndex = [[self xueliArr] indexOfObject:self.xueliLa.text];
    }
    XMWeakSelf(self);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        XMStrongSelf(self);
        self.xueliLa.text = kStringFormat(@"%@", resultModel.value);
        
    };
    stringPickerView.pickerStyle = [self pickerStyle];
    [stringPickerView show];
}

- (IBAction)zhiyeAct:(UIButton *)sender {
    [self.view endEditing:YES];
    
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc] init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = @"选择职业";
    stringPickerView.dataSourceArr = [self zhiyeArr];
    if (self.zhiyeLa.text.length > 0) {
        stringPickerView.selectIndex = [[self zhiyeArr] indexOfObject:self.zhiyeLa.text];
    }
    XMWeakSelf(self);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        XMStrongSelf(self);
        self.zhiyeLa.text = kStringFormat(@"%@", resultModel.value);
    };
    stringPickerView.pickerStyle = [self pickerStyle];
    [stringPickerView show];
}

- (IBAction)luzhiAct:(UIButton *)sender {
    if ([self.luzhiLa.text isEqualToString:@"去录制"]) {
        [self showRecordView];
    } else if ([self.luzhiLa.text isEqualToString:@"已完成"]) {
        [self showTipView];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BRPickerStyle *)pickerStyle {
    BRPickerStyle *customStyle = [[BRPickerStyle alloc] init];
    customStyle.pickerTextColor = kHEX(@"999999");
    customStyle.selectRowTextColor = kAppMainColor;
    customStyle.separatorColor = kAppMainColor;
    customStyle.topCornerRadius = 20;
    customStyle.titleTextFont = kUIFont(16);
    customStyle.titleTextColor = UIColor.blackColor;
    return customStyle;
}


- (NSMutableArray *)zhiyeArr {
    NSMutableArray *occupationArray = @[
                                        @"白领",
                                        @"飞行员",
                                        @"工程师",
                                        @"公务员",
                                        @"护士",
                                        @"教师",
                                        @"警察",
                                        @"会计",
                                        @"律师",
                                        @"模特",
                                        @"企业高管",
                                        @"私企老板",
                                        @"设计师",
                                        @"行政",
                                        @"医生",
                                        @"自由职业"]
        .mutableCopy;
    return occupationArray;
}

- (NSMutableArray *)xueliArr {
    NSMutableArray *occupationArray = @[
        @"博士",
        @"硕士",
        @"本科",
        @"大专",
        @"高中",
        @"小学"]
        .mutableCopy;
    return occupationArray;
}

- (void)showTipView {
    if ([GKCover hasCover]) {
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self coverTipView];
        }];
    } else {
        [self coverTipView];
    }
}

- (void)coverTipView {
    [GKCover coverFrom:self.view contentView:self.tipView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
}

- (XMLoginPrivacyTipView *)tipView {
    if (_tipView == nil) {
        _tipView = [XMLoginPrivacyTipView viewFromXib];
        _tipView.size = CGSizeMake(295, 177);
        [_tipView setupVoice];
    }
    return _tipView;
}

- (void)showRecordView {
    XMLoginRecordView *recordView = [XMLoginRecordView viewFromXib];
    recordView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    [GKCover coverFrom:self.view
           contentView:recordView
                 style:GKCoverStyleTranslucent
             showStyle:GKCoverShowStyleCenter
         showAnimStyle:GKCoverShowAnimStyleNone
         hideAnimStyle:GKCoverHideAnimStyleNone
              notClick:YES];
    
    XMWeakSelf(self);
    recordView.completeBlock = ^(NSString *_Nonnull filePath, NSInteger voiceLength) {
        XMStrongSelf(self);
        [GKCover hideCover];
        self.luzhiLa.text = @"已完成";
        self.voicePath = filePath;
        self.voiceLength = voiceLength;
        };
    
    recordView.closeBlock = ^{
        [GKCover hideCover];
    };
}

- (XMHeightChooseView *)heightChooseV {
    if (_heightChooseV == nil) {
        _heightChooseV = [XMHeightChooseView viewFromXib];
        _heightChooseV.size = CGSizeMake(kScreenWidth, 288);
    }
    return _heightChooseV;
}

- (void)showHeightChooseV {
    [GKCover coverFrom:self.view contentView:self.heightChooseV style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleBottom) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
    
}

@end
